<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {font-family: Arial, sans-serif;background-color: #f8f8f8;}
        .container {
            max-width: 400px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);}
        h2 {text-align: center;}
        label {display: block;margin-bottom: 5px;}
        input[type="text"],
        input[type="password"],
        select,
        input[type="email"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
            font-size: 16px; }
        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;}
        .error {
            color: #ff0000;
            margin-top: 5px;
        }
    </style>
</head>
<body>
  <div class="container">
    <h2>表单验证</h2>
    <form id="myForm">
      <label for="username">用户名:</label>
      <input type="text" id="username" name="username" placeholder="请输入用户名">
      <label for="jl" id="lab" style="color: red; font-size: 12px;margin-top: -10px;"></label>

      <label for="password">密码:</label>
      <input type="password" id="password" name="password" placeholder="请输入密码">
      <label for="jl" id="lap" style="color: red; font-size: 12px;margin-top: -10px;"></label>

      <label for="qrpassword">确认密码:</label>
      <input type="password" id="qrpassword" name="qrpassword" placeholder="请确认密码">
      <label for="jl" id="qrlap" style="color: red; font-size: 12px;margin-top: -10px;"></label>

      <label for="gender">性别:</label>
      <select id="gender" name="gender">
        <option value="">请选择性别</option>
        <option value="男">男</option>
        <option value="女">女</option>
      </select>

      <label for="email">Email:</label>
      <input type="email" id="email" name="email" placeholder="请输入Email">

      <button type="submit">提交</button>
    </form>
  </div>

  <script>
    document.getElementById("myForm").addEventListener("submit", function(event) {
      event.preventDefault();
      if (aa()) {
        // 表单验证通过，可以进行表单提交操作
        alert("表单验证通过！");
      }
    });

    function aa() {
      var username = document.getElementById("username").value;
      var password = document.getElementById("password").value;
      var qrpassword = document.getElementById("qrpassword").value;
      var gender = document.getElementById("gender").value;
      var email = document.getElementById("email").value;
      var lab=document.getElementById("lab");
      var lap=document.getElementById("lap");
      var qrlap=document.getElementById("qrlap");
      var isValid = true;

      if (username.length < 5) {
        isValid = false;
        document.getElementById("username").classList.add("error");
        lab.innerHTML="用户名不能为空并且不能小于5"
      } else {
        document.getElementById("username").classList.remove("error");
      }
      if (username.length >= 5) {
        lab.innerHTML=" "
      } 
      
      if (password.length < 5) {
        isValid = false;
        document.getElementById("password").classList.add("error");
        lap.innerHTML="密码不能为空并且不能小于5"
      } else {
        document.getElementById("password").classList.remove("error");
      }
      if (password.length >=5) {
        lap.innerHTML=" "
      }
      if(qrpassword==""){
        isValid = false;
        document.getElementById("qrpassword").classList.add("error");
        qrlap.innerHTML="确认密码不能为空"
      }
      if (password !== qrpassword) {
        isValid = false;
        document.getElementById("qrpassword").classList.add("error");
        qrlap.innerHTML="两个密码不一样"
      } else {
        document.getElementById("qrpassword").classList.remove("error");
      }
      if (password == qrpassword) {
        qrlap.innerHTML=" "
      } 
      if (!gender) {
        isValid = false;
        document.getElementById("gender").classList.add("error");
      } else {
        document.getElementById("gender").classList.remove("error");
      }

      if (!email) {
        isValid = false;
        document.getElementById("email").classList.add("error");
      } else {
        document.getElementById("email").classList.remove("error");
      }
      return isValid;
    }
  </script>
</body>
</html>